package com.token.serviceImpl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.token.bean.Admin;
import com.token.mapper.AdminMapper;
import com.token.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.token.util.sms.EhcacheUtil;
import com.token.util.sms.SmsMark;
import com.token.util.sms.SmsUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台 服务实现类
 * </p>
 *
 * @author 冯政尚
 * @since 2019-07-30
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {


    @Override
    public boolean login(String userName, String passWord, HttpSession session) {
        Map map = new HashMap<>();
        map.put("user_name",userName);
        map.put("pass_word",passWord);
        List<Admin> list = baseMapper.login(map);
        if (list.size()>0){
            session.setAttribute("user",list.get(0));
            return true;
        }
        return false;
    }


    /**
     * 获取验证码
     * @param phone
     * @return
     */
    @Override
    public Map SendSMS(Long phone) {
        Map map = new HashMap();
        SendSmsResponse response;
        String code= SmsMark.getCode();
        try {
            response =  SmsUtil.sendSms(phone+"", SmsMark.SMS_MARK,code);
        } catch (ClientException e) {
            e.printStackTrace();
            map.put("code",2);
            map.put("mag","服务器出错，请稍后再试");
            return  map;
        }
        //验证码获取成功后
        System.out.println("123:"+response.getMessage());
        System.out.println("123:"+response.getCode());
        if(response.getMessage().equals("触发小时级流控Permits:5")){
            map.put("code",-1);
            map.put("msg","一个小时只能发送5条短信");
            return map;
        }else if(response.getMessage().equals("触发分钟级流控Permits:1")){
            map.put("code",-2);
            map.put("msg","一分钟只能发送一条短信");
            return map;
        }else if(response.getMessage().equals("触发天级流控Permits:10")){
            map.put("code",-3);
            map.put("msg","一天只能发送十条短信");
            return map;
        } else if(response.getMessage().equals("OK")){
                //存进cache缓存  180后过期  当访问过一次后 60秒内没有再此访问 同样过期 缓存区的key设置成数字加字符串 否则获取不到
                EhcacheUtil.put("SMS",phone+"Data",code);
                System.out.println("用户手机号: "+phone);
                System.out.println("用户输入的验证码: "+code);
                System.err.println("----------------------------------");
                map.put("code",0);
                map.put("msg","获取验证码成功");
                return  map;
        }else{
            map.put("code",1);
            map.put("msg","获取短信验证码失败");
            return  map;
        }

    }



    /**
     * @param code
     * @param phone
     * @return 和缓存中的验证码比较
     */
    @Override
    public boolean equalsCache(String code,String phone) {
        //从缓存中获取六位验证码 当访问过一次后 60秒内没有再此访问会过期  缓存区的key设置成数字加字符串 否则获取不到
        String cacheCode= EhcacheUtil.get("SMS",phone+"Data")+"";
        System.out.println("缓存中的验证码："+cacheCode);
        System.out.println("用户输入的验证码："+code);
        System.out.println("用户手机号："+phone);
        System.err.println("----------------------------------");
        if (code.equals(cacheCode)){
            return true;
        }
        return false;
    }


}
